package com.cqc.greendao05;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cqc.greendao05.adapter.MyAdapter;
import com.cqc.greendao05.bean.PersonInfo;
import com.cqc.greendao05.dao.gen.PersonInfoDao;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et1;
    private EditText et2;
    private EditText et3;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btn7;
    private Button btn8;
    private RecyclerView recyclerView;
    private PersonInfoDao dao;
    private List<PersonInfo> list = new ArrayList<>();
    private MyAdapter adapter;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        initViews();
    }

    private void initViews() {
        toast = Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT);

        dao = GreenDaoManager.getDaoSession().getPersonInfoDao();
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(MainActivity.this, DividerItemDecoration.HORIZONTAL));
        adapter = new MyAdapter(list);
        recyclerView.setAdapter(adapter);
    }

    private void findViews() {
        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        et3 = (EditText) findViewById(R.id.et3);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);
        btn7 = (Button) findViewById(R.id.btn7);
        btn8 = (Button) findViewById(R.id.btn8);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                insert();
                break;
            case R.id.btn2:
                delete();
                break;
            case R.id.btn3:
                update();//根据id，改了name
                break;
            case R.id.btn4:
                query();
                break;
            case R.id.btn5:
                deleteAll();
                break;
            case R.id.btn6:
                deleteList();
                break;
            case R.id.btn7:

                break;
            case R.id.btn8:

                break;

        }
    }

    private void deleteList() {
        //删除姓名相同的集合
        String name = et2.getText().toString().trim();
        List<PersonInfo> userList = dao.queryBuilder().where(PersonInfoDao.Properties.Name.eq(name)).build().list();
        for (PersonInfo info : userList) {
            dao.delete(info);
        }
    }

    private void update() {
        //现根据条件数据库中查询到这个数据，再进行操作（删除）
        String id = et1.getText().toString().trim();
        PersonInfo info = dao.queryBuilder().where(PersonInfoDao.Properties.Id.eq(id)).build().unique();
        info.setName(info.getName() + "update");
        dao.update(info);
    }

    private void deleteAll() {
        dao.deleteAll();//删除全部数据
    }

    private void delete() {
        String id = et1.getText().toString().trim();
//        long id = Long.parseLong(idStr);
        //现根据条件数据库中查询到这个数据，再进行操作（删除）
        PersonInfo info = dao.queryBuilder().where(PersonInfoDao.Properties.Id.eq(id)).build().unique();
        dao.delete(info);
    }

    private void query() {
        List<PersonInfo> usersList = dao.queryBuilder().build().list();
        list.clear();
        list.addAll(usersList);
        adapter.notifyDataSetChanged();
    }

    private void insert() {
        String idStr = et1.getText().toString().trim();
        long id = 0;
        if (TextUtils.isEmpty(idStr)) {
            showToast("id不能为空");
            return;
        } else {
            id = Long.parseLong(idStr);
        }
        String name = et2.getText().toString().trim();
        String ageStr = et3.getText().toString().trim();
        int age = 0;
        if (!TextUtils.isEmpty(ageStr)) {
            age = Integer.parseInt(ageStr);
        }
        PersonInfo info = new PersonInfo(id, name, age);
        dao.insert(info);
    }

    public void showToast(String content) {
        toast.setText(content);
        toast.show();
    }
}
